﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace NetMove01
{
    public class NetServer : NetBase
    {

        public override void Start(ERole er)
        {
            base.Start(ERole.Server);
            IPAddress ipAddr = IPAddress.Parse("192.168.1.77");
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
            Socket sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sListener.Bind(ipEndPoint);
            sListener.Listen(10);
            Handle = sListener.Accept();
            Handle.Blocking = false;
            
        }

        public override void Send(SendData sd) 
        {
            byte[] bytes = new byte[8];
            if (Handle != null)
            if (Handle.Connected) 
            {
                Handle.Send(sd.ToBytes());                   
            }

            
        }

        public override void Receive(out SendData sd) 
        {
            sd = new SendData();
  

            byte[] bytes = new byte[8];

            if (Handle != null)
            if (Handle.Connected)
                if (Handle.Poll(0, SelectMode.SelectRead))
                {
                    int bytesRec = Handle.Receive(bytes);


                    sd.x = BitConverter.ToInt32(bytes, 0);
                    sd.y = BitConverter.ToInt32(bytes, 4);


                }   
        }

        
    }
}
